//
//  PosterCell.m
//  HWSGW
//
//  Created by avachen on 15/11/7.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "PosterCell.h"
#import "Movie.h"
#import "UIImageView+WebCache.h"
#import "MovieDetailView.h"

@implementation PosterCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //创建子视图1
        [self _createView];
        
        _left = YES;
        
    }
    return self;
}
- (void)_createView{
    //1、创建图片视图
    _imgView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
    _imgView.backgroundColor = [UIColor yellowColor];
    _imgView.transform = CGAffineTransformMakeScale(0.95, 0.95);
    _imgView.tag = 200;
    
    [self.contentView addSubview:_imgView];
    
    //2、创建详情页面
   
    _detailView  = [[[NSBundle mainBundle] loadNibNamed:@"MovieDetailView" owner:self options:nil]lastObject];
    
    _detailView.backgroundColor = [UIColor grayColor];
    _detailView.transform = CGAffineTransformMakeScale(0.95, 0.95);
    _detailView.tag = 201;
    
    [self.contentView insertSubview:_detailView belowSubview:_imgView];
    
    
}
- (void)setMovie:(Movie *)movie
{
    if (_movie != movie) {
        _movie = movie;
        
        NSString *largeUrl = [movie.images objectForKey:@"large"];
        [_imgView sd_setImageWithURL:[NSURL URLWithString:largeUrl]];
        
        [_detailView setMovie:movie];
        
        
    }
}
- (void)flipCell
{
    NSInteger index1 = [self.contentView.subviews indexOfObject:_imgView];
    NSInteger index2 = [self.contentView.subviews indexOfObject:_detailView];
    
    UIViewAnimationOptions flip = _left ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight;
    
    [UIView transitionWithView:self.contentView duration:0.4 options:flip animations:^{
        [self.contentView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
        
    } completion:NULL];
    
    _left = !_left;
    
}
@end
